<template>
  <!-- <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"> -->
    <div id="mesCodeMasterM-add-or-update">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="字典类型" prop="codetype">
      <el-input v-model="dataForm.codetype" placeholder="类型" :disabled="$route.params.codetype"></el-input>
    </el-form-item>
    <el-form-item label="字典名称">
      <el-input v-model="dataForm.codename" placeholder="字典名称" :disabled="$route.params.copy==='copy'"></el-input>
    </el-form-item>
    <el-form-item label="编号" prop="code">
      <el-input v-model="dataForm.code" placeholder="编号" :disabled="$route.params.codetype&&$route.params.copy!=='copy'"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="顺序">
      <el-input v-model="dataForm.sort" placeholder="顺序(请输入数字格式)"></el-input>
    </el-form-item>
    <el-form-item label="value1">
      <el-input v-model="dataForm.value1" placeholder="value1"></el-input>
    </el-form-item>
    <el-form-item label="value2">
      <el-input v-model="dataForm.value2" placeholder="value2"></el-input>
    </el-form-item>
    <el-form-item label="value3">
      <el-input v-model="dataForm.value3" placeholder="value3"></el-input>
    </el-form-item>
    <el-form-item label="value4">
      <el-input v-model="dataForm.value4" placeholder="value4"></el-input>
    </el-form-item>
    <el-form-item label="value5">
      <el-input v-model="dataForm.value5" placeholder="value5"></el-input>
    </el-form-item>
    </el-form>
    <div class="footer">
    <!-- <span slot="footer" class="dialog-footer"> -->
      <el-button type="primary" @click="dataFormSubmit()">保存</el-button>
      <el-button @click="cancle">取消</el-button>
    <!-- </span> -->
    </div>
    </div>
  <!-- </el-dialog> -->
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        codetype: '',
        codename: '',
        code: '',
        name: '',
        sort: '',
        value1: '',
        value2: '',
        value3: '',
        value4: '',
        value5: ''
      },
      pageIndex: 1,
      pageSize: 10,
      dataRule: {
        codetype: [
          { required: true, message: '类型不能为空', trigger: 'blur' }
        ],
        code: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        sort: [{ required: true, message: '顺序不能为空', trigger: 'blur' }],
        value1: [
          { required: true, message: 'value1不能为空', trigger: 'blur' }
        ],
        value2: [
          { required: true, message: 'value2不能为空', trigger: 'blur' }
        ],
        value3: [
          { required: true, message: 'value3不能为空', trigger: 'blur' }
        ],
        value4: [
          { required: true, message: 'value4不能为空', trigger: 'blur' }
        ],
        value5: [{ required: true, message: 'value5不能为空', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.init(this.$route.params)
  },
  methods: {
    init(params) {
      console.log(params)
      this.dataForm.codetype = params.codetype || 0
      // this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (params.codetype) {
          this.$http({
            url: this.$http.adornUrl('/basic/mescodemasterm/info'),
            method: 'get',
            params: this.$http.adornParams({
              codetype: params.codetype,
              code: params.code
            })
          }).then(({ data }) => {
            console.log(data)
            if (data && data.code === 0) {
              if (params.copy === 'copy') {
                this.dataForm.codetype = data.mesCodeMasterM.codetype
                this.dataForm.codename = data.mesCodeMasterM.codename
              } else {
                this.dataForm = data.mesCodeMasterM
              }
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/basic/mescodemasterm/${
                !this.$route.params.codetype ||
                this.$route.params.copy === 'copy'
                  ? 'save'
                  : 'update'
              }`
            ),
            method: 'post',
            data: this.$http.adornData({
              codetype: this.dataForm.codetype || undefined,
              codename: this.dataForm.codename,
              code: this.dataForm.code,
              name: this.dataForm.name,
              sort: this.dataForm.sort,
              value1: this.dataForm.value1,
              value2: this.dataForm.value2,
              value3: this.dataForm.value3,
              value4: this.dataForm.value4,
              value5: this.dataForm.value5
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  // this.visible = false
                  // this.$emit('refreshDataList')
                  eventBus.$emit('closeCurrentPage')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    // 取消
    cancle() {
      eventBus.$emit('closeCurrentPage')
    }
  }
}
</script>
<style scoped lang="scss">
#mesCodeMasterM-add-or-update {
  width: 60%;
  margin: 0 auto;
  .footer {
    text-align: center;
  }
}
</style>